package com.ruoyi.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.teaching.domain.*;
import com.ruoyi.teaching.mapper.*;
import com.ruoyi.teaching.service.CheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class CheckServiceImpl implements CheckService {

    @Autowired
    private PlatformEExamQuestionMapper platformEExamQuestionMapper;

    @Autowired
    private PlatformTTaskQuestionMapper platformTTaskQuestionMapper;

    @Autowired
    private PlatformQQuestionMapper platformQQuestionMapper;

    @Autowired
    private PlatformQKnowledgeMapper platformQKnowledgeMapper;

    @Autowired
    private PlatformPCommentMapper platformPCommentMapper;


    @Override
    public String questionIsUse(int id) {
        List<PlatformEExamQuestion> platformEExamQuestion = platformEExamQuestionMapper.selectByQuestionId(id);
        // 考试题库是空的就再去查作业题库
        if (!CollectionUtils.isEmpty(platformEExamQuestion)) {
            PlatformQQuestion platformQQuestion = platformQQuestionMapper.selectById(id);
            return platformQQuestion.getQuestionName();
        } else {
            List<PlatformTTaskQuestion> platformTTaskQuestions = platformTTaskQuestionMapper.selectByQuestionId(id);
            if (!CollectionUtils.isEmpty(platformTTaskQuestions)) {
                PlatformQQuestion platformQQuestion = platformQQuestionMapper.selectById(id);
                return platformQQuestion.getQuestionName();
            }
        }
        return "";
    }

    @Override
    public String checkQuestionInKnowledge(Long id) {
        List<PlatformQQuestion> platformQQuestions = platformQQuestionMapper.selectByknowledgeId(id);
        if (org.apache.commons.collections4.CollectionUtils.isEmpty(platformQQuestions)) {
            return "";
        } else {
            PlatformQKnowledge platformQKnowledge = platformQKnowledgeMapper.selectById(id);
            return platformQKnowledge.getKnowledgeName();
        }
    }

    @Override
    public boolean checkComment(PlatformPComment platformPComment) {
        Long userId = SecurityUtils.getUserId();
        // 锁定用户 ID，防止并发问题
        synchronized (userId.toString().intern()) {
            QueryWrapper<PlatformPComment> platformPCommentQueryWrapper = new QueryWrapper<>();
            platformPCommentQueryWrapper.eq("comment_type", platformPComment.getCommentType());
            platformPCommentQueryWrapper.eq("comment_project_id", platformPComment.getCommentProjectId());
            platformPCommentQueryWrapper.eq("comment_person_id", userId);
            List<PlatformPComment> platformPComments = platformPCommentMapper.selectList(platformPCommentQueryWrapper);
            return org.apache.commons.collections4.CollectionUtils.isNotEmpty(platformPComments);
        }
    }
}
